package com.googlecode.afdbaccess.web.portal.webcontent.login.login_form_data;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import org.jboss.resteasy.annotations.cache.Cache;

import com.googlecode.afdbaccess.web.common.exception.GuiLayerException;
import com.googlecode.afdbaccess.web.common.interceptors.RequestLogged;
import com.googlecode.afdbaccess.web.common.json.ExtJsJsonResponse;

@RequestLogged
@Path("login/login-form-data")
public class LoginPostRest {
    @POST
    @Cache(isPrivate=true)
    @Produces("application/json")
    public ExtJsJsonResponse login(@Context HttpServletRequest request, @FormParam("username") String username, @FormParam("password") String password) throws Exception {
        try {
            request.login(username, password);
            return new ExtJsJsonResponse();
        } catch(ServletException se) {
            throw new GuiLayerException("Login failed", se.getLocalizedMessage(), se);
        }
    }
}
